package com.cg.asm.client;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cg.asm.client.constans.ConstantsEnum;
import com.cg.asm.client.exception.CommonJsonException;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class CommonUtil {

    /**
     * 返回一个returnData为空对象的成功消息的json
     *
     * @return
     */
    public static JSONObject successJson() {
        return successJson(new JSONObject());
    }

    /**
     * 返回一个返回码为100的json
     *
     * @param returnData
     * @return
     */
    public static JSONObject successJson(Object returnData) {
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnType", "success");
        resultJson.put("returnCode", ConstantsEnum.REQUEST_SUCCESS.getKey());
        resultJson.put("returnMsg", ConstantsEnum.REQUEST_SUCCESS.getValue());
        resultJson.put("returnData", JSON.toJSON(returnData));
        return resultJson;
    }

    /**
     * 返回一个返回码为100的json
     *
     * @param returnData
     * @return
     */
    public static JSONObject successJsonAndSid(Object returnData, String sid) {
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnType", "success");
        resultJson.put("returnCode", ConstantsEnum.REQUEST_SUCCESS.getKey());
        resultJson.put("returnMsg", ConstantsEnum.REQUEST_SUCCESS.getValue());
        resultJson.put("returnData", JSON.toJSON(returnData));
        resultJson.put("serviceSid", sid);
        return resultJson;
    }

    /**
     * 返回错误信息JSON
     *
     * @return
     */
    public static JSONObject errorJson() {
        return errorJson(
                ConstantsEnum.RESPONSE_E_AAAA.getKey(),
                ConstantsEnum.RESPONSE_E_AAAA.getValue(),
                new JSONObject()
        );
    }

    /**
     * 返回自定义错误信息
     *
     * @return
     */
    public static JSONObject errorJson(String msg) {
        return errorJson("000000", msg, new JSONObject()
        );
    }

    /**
     * 返回错误信息JSON
     *
     * @param returnCode
     * @param returnMsg
     * @return
     */
    public static JSONObject errorJson(String returnCode, String returnMsg) {
        return errorJson(returnCode, returnMsg, new JSONObject());
    }

    /**
     * 返回错误信息JSON
     *
     * @param returnCode
     * @param returnMsg
     * @return
     */
    public static JSONObject errorJsonAndSid(String returnCode, String returnMsg, String sid) {
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnType", "error");
        resultJson.put("returnCode", returnCode);
        resultJson.put("returnMsg", returnMsg);
        resultJson.put("returnData", new JSONObject());
        resultJson.put("serviceSid", sid);
        return resultJson;
    }

    /**
     * 返回错误信息JSON
     *
     * @param returnCode
     * @param returnMsg
     * @param jsonObject
     * @return
     */
    public static JSONObject errorJson(String returnCode, String returnMsg, Object jsonObject) {
        JSONObject resultJson = new JSONObject();
        resultJson.put("returnType", "error");
        resultJson.put("returnCode", returnCode);
        resultJson.put("returnMsg", returnMsg);
        resultJson.put("returnData", jsonObject);
        return resultJson;
    }

    /**
     * 将request参数值转为json
     *
     * @param request
     * @return
     */
    public static JSONObject request2Json(HttpServletRequest request) {
        JSONObject requestJson = new JSONObject();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String[] pv = request.getParameterValues(paramName);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pv.length; i++) {
                if (pv[i].length() > 0) {
                    if (i > 0) {
                        sb.append(",");
                    }
                    sb.append(pv[i]);
                }
            }
            requestJson.put(paramName, sb.toString());
        }
        return requestJson;
    }

    /**
     * 将request转JSON
     * 并且验证非空字段
     *
     * @param request
     * @param requiredColumns
     * @return
     */
    public static JSONObject convert2JsonAndCheckRequiredColumns(HttpServletRequest request, String requiredColumns) {
        JSONObject jsonObject = request2Json(request);
        hasAllRequired(jsonObject, requiredColumns);
        return jsonObject;
    }

    /**
     * 验证是否含有全部必填字段
     *
     * @param requiredColumns 必填的参数字段名称 逗号隔开 比如"userId,name,telephone"
     * @param jsonObject
     * @return
     */
    public static void hasAllRequired(final JSONObject jsonObject, String requiredColumns) {
        if (StringUtils.isNotEmpty(requiredColumns)) {
            //验证字段非空
            String[] columns = requiredColumns.split(",");
            String missCol = "";
            for (String column : columns) {
                Object val = jsonObject.get(column.trim());
                if (null == val || "".equals(val)) {
                    missCol += column + "  ";
                }
            }
            if (StringUtils.isNotEmpty(missCol)) {
                jsonObject.clear();
                jsonObject.put("returnCode", ConstantsEnum.RESPONSE_E_AAAB.getKey());
                jsonObject.put("returnMsg", ConstantsEnum.RESPONSE_E_AAAB.getValue() + ":" + missCol.trim());
                jsonObject.put("returnData", new JSONObject());
                throw new CommonJsonException(jsonObject);
            }
        }
    }
}
